﻿// Parts Copyright 2008 Newgrove Consultants Limited
// Modified By John Diss 
// From original work:
//
// Copyright 2006 - Ricardo Stuven (rstuven@gmail.com)
//
// This file is part of MsSqlSpatial.
// MsSqlSpatial is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// MsSqlSpatial is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public License
// along with MsSqlSpatial; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 

using System.Data.SqlTypes;
using GeoAPI.Geometries;
using Microsoft.SqlServer.Server;
using MsSqlSpatial;

public partial class UserDefinedFunctions
{
    /// <summary>
    /// Returns <c>true</c> if this geometry is covered by the specified geometry.
    /// <para>
    /// The <c>CoveredBy</c> predicate has the following equivalent definitions:
    ///     - Every point of this geometry is a point of the other geometry.
    ///     - The DE-9IM Intersection Matrix for the two geometries is <c>T*F**F***</c> or <c>*TF**F***</c> or <c>**FT*F***</c> or <c>**F*TF***</c>.
    ///     - <c>g.Covers(this)</x> (<c>CoveredBy</c> is the inverse of <c>Covers</c>).
    /// </para>
    /// Note the difference between <c>CoveredBy</c> and <c>Within</c>: <c>CoveredBy</c> is a more inclusive relation.
    /// </summary>
    /// <param name="g">The <c>Geometry</c> with which to compare this <c>Geometry</c></param>.
    /// <returns><c>true</c> if this <c>Geometry</c> is covered by <paramref name="g" />.</returns>
    [SqlFunction(IsDeterministic = true)]
    public static SqlBoolean CoveredBy(SqlBytes ewkb1, SqlBytes ewkb2)
    {
        IGeometry geometry1 = ConvertGeometry.ToGeometry(ewkb1);
        if (geometry1 != null)
        {
            IGeometry geometry2 = ConvertGeometry.ToGeometry(ewkb2);
            if (geometry2 != null)
            {
                if (geometry1.Srid != geometry2.Srid)
                    throw new DifferentSridException(geometry1.Srid, geometry2.Srid);
                return new SqlBoolean(geometry2.Covers(geometry1));
            }
        }
        return SqlBoolean.Null;
    }
} ;